package com.example.test2.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.test2.R;
import com.example.test2.db.entity.Buy;
import com.example.test2.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class BuyAdapter  extends RecyclerView.Adapter<BuyAdapter.ViewHolder> {

    private Context context;
    private List<Buy> list = new ArrayList<>();

    public BuyAdapter(Context context, List<Buy> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<Buy> list) {
        this.list = list;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_buy, parent, false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Buy purchase = list.get(position);
        String name = purchase.getName();
        holder.tvName.setText(name+" "+purchase.getType());

        holder.tvNeed.setText(purchase.getNeed());
        holder.tvFrom.setText("期望发货地："+purchase.getFromAddress());

        holder.llMain.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEdit(position);
            }
        });
        holder.btBid.setOnClickListener(view -> {
            ToastUtils.getInstance().show(context, "报价已结束，不能报价！");
        });
    }




    @Override
    public int getItemCount() {
        return list.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout llMain;
        private TextView tvName;
        private Button btBid;
        private TextView tvNeed;
        private TextView tvFrom;


        public ViewHolder(View itemView) {
            super(itemView);
            llMain = itemView.findViewById(R.id.ll_main);
            tvNeed= itemView.findViewById(R.id.tv_need);
            tvName = itemView.findViewById(R.id.tv_name);
            tvFrom = itemView.findViewById(R.id.tv_from);
            btBid = itemView.findViewById(R.id.bt_go);
        }
    }
    private OnClickListener listener;

    public void setListener(OnClickListener listener) {
        this.listener = listener;
    }

    public interface OnClickListener {
        void onEdit(int pos);
    }
}



